package com.laikan.reader.database.db;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wx
 * @version 创建时间：2015-12-14 下午5:53:25
 * @E-mail wx1343571153@163.com
 */

public class ThreadPool {

    private static ThreadPool instance;
    private ExecutorService executorService;

    public ThreadPool() {
        if (null == executorService) {
            executorService = Executors.newFixedThreadPool(1);
        }
    }

    public static ThreadPool Instance() {
        if (null == instance) {
            synchronized (ThreadPool.class) {
                if (null == instance) {
                    instance = new ThreadPool();
                }
            }
        }
        return instance;
    }

    /**
     * 添加任务
     *
     * @param runn
     */
    public void addTask(Runnable runn) {
        if (executorService.isTerminated()) {
            executorService = Executors.newFixedThreadPool(1);
        }
        executorService.execute(runn);
    }

    /**
     * 关闭线程池
     */
    public void shutdown() {
        if (!executorService.isShutdown()) {
            executorService.shutdown();
        }

    }
}
